package com.example.helloword.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import com.example.helloword.R;
import com.example.helloword.background.BackgroudThreadpoolService;
import com.example.helloword.background.EventManager;
import com.example.helloword.background.SourceListener;
import com.example.helloword.bean.Book;
import com.example.helloword.bean.Chapter;
import com.example.helloword.request.RequestBookController;

import java.util.List;

public class PagesInReadActivity extends AppCompatActivity {
    private List<Chapter> chapters;
    private int  current_chatper;
    private PagesInReadView pagesInReadView;
    private boolean isNext=true ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pages_in_read);
        Intent intent = getIntent();
        Book book = (Book)intent.getSerializableExtra("book");
        chapters=book.getChapters();
        current_chatper = intent.getIntExtra("current_chatper",0);
        pagesInReadView=findViewById(R.id.pi_v);

        BackgroudThreadpoolService.getInstance().getTheadpool().submit(new RequestBookController(RequestBookController.CONTENT,chapters.get(current_chatper).getChapter_url(),""));

        EventManager.getInstance().addUserListListener(new SourceListener() {
            @Override
            public void getBookList(List book_list) {

            }

            @Override
            public void getChapterList(List chapter_list) {

            }

            @Override
            public void getContent(String content) {

                pagesInReadView.pagesTobook(content,isNext);
            }
        });

        pagesInReadView.setSwitchChapterListener(new PagesInReadView.SwitchChapterListener() {
            @Override
            public void SwitchChapter(int type,boolean is) {
                isNext=is;
                switch (type){
                    case PagesInReadView.SwitchChapterListener
                            .NEXT:{
                        current_chatper++;
                        break;
                    }
                    case PagesInReadView.SwitchChapterListener
                            .PREVIOUS:{
                        current_chatper--;
                        break;
                    }
                }
                if (current_chatper>=0&&current_chatper<=chapters.size()-1){
                    BackgroudThreadpoolService.getInstance().getTheadpool().submit(new RequestBookController(RequestBookController.CONTENT,chapters.get(current_chatper).getChapter_url(),""));
                }else {
                    if (current_chatper<0){
                        current_chatper++;
                    }else {
                        current_chatper--;
                    }
                }
            }
        });

    }
}